
import { ResouceManager } from "./ResouceManager";
import { StoreManager } from "./StoreManager";
import { Fun } from "../Utility/dx/Fun";

export class CMDManager
{
    private static cmds:{[key:string]:any} = {};
    private static isAddListener:boolean = false;
    private static editNode:cc.Node;
    private static editLabel:cc.EditBox;
    private static needCMD:string = "";
    public static AddCMD(cmd:string,fun:Function,target?:any,inNeedParams?:boolean):void
    {
        this.cmds[cmd] = {fun:fun,target:target,inNeedParams:inNeedParams};
        if(this.isAddListener)return;
        this.isAddListener = true;
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown,this);
    }

    private static onKeyDown(evt):void
    {
        if(evt.keyCode==13)
        {
            if(this.editNode==null||this.editNode.parent ==null)
            {
                if(this.editNode==null)this.createCMDEdit();
                else 
                {
                    this.editNode.color = cc.color(Math.random()*255,Math.random()*255,Math.random()*255);
                    cc.director.getScene().getChildByName("Canvas").addChild(this.editNode);
                    this.editLabel.setFocus();
                }
            }else this.onExcuteCMD();
        }
    }
    private static createCMDEdit():void
    {
        let lableNode = new cc.Node();
        this.editLabel = lableNode.addComponent(cc.EditBox);
        let sp = StoreManager.NewNode(cc.Sprite);

        this.editNode = sp.node;
        ResouceManager.LoadSpriteFrame("General/singleColor",sp);//Textures\General
        sp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
        sp.type = cc.Sprite.Type.SLICED
        sp.node.width = 350;
        sp.node.height = 200;
        sp.node.color = cc.color(Math.random()*255,Math.random()*255,Math.random()*255);
        sp.node.opacity = 125;
        lableNode.setContentSize(250,45);
        sp.node.addChild(lableNode);
        lableNode.opacity = 125;
        this.editLabel.fontSize = 40;
        this.editLabel.maxLength = 50;
        this.editLabel.placeholder = " ";
        this.editLabel.fontColor = cc.Color.BLACK;
        this.editLabel.inputMode = cc.EditBox.InputMode.SINGLE_LINE;
        if(sp.spriteFrame==null)
        {
            ResouceManager.LoadSpriteFrame("General/singleColor",null,Fun(()=>this.editLabel.backgroundImage = sp.spriteFrame));
            //ResouceManager.LoadSpriteFrame("General/singleColor",null,null,()=>this.editLabel.backgroundImage = sp.spriteFrame);
        }else
        {
            this.editLabel.backgroundImage = sp.spriteFrame;
        }
        
        lableNode.on('editing-return',this.onEditEnter,this);
        this.editLabel.stayOnTop = true;
        this.editLabel.setFocus();
        cc.director.getScene().getChildByName("Canvas").addChild(sp.node);
    }

    private static onEditEnter(evt?:any):void
    {
        this.onExcuteCMD();
    }

    private static onExcuteCMD():void
    {
        let str = this.editLabel.string;
        this.editLabel.string = "";
        if(this.editNode.parent)
            this.editNode.removeFromParent();
        if(str.trim().length==0)return;
        if(this.needCMD=="")
        {
            if(this.IsNeedParam(str))this.needCMD=str;
            else this.Excect(str);
        }else
        {
            let args:any[] = str.split(",");
            this.Excect(this.needCMD,args);
            this.needCMD = "";
        }
    }
    public static Excect(cmd:string,args?:any):void
    {
        let o = this.cmds[cmd];
        if(o==null)return;
        o.fun.apply(o.target,args);
    }

    public static IsNeedParam(cmd:string):boolean
    {
        let o = this.cmds[cmd];
        if(o==null)return false;
        return o.inNeedParams;
    }
}
